﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 问题：一次性创建对象并设置其属性
/// 解决方案：使用对象或集合初始化器
/// </summary>

namespace Ex02
{
    //class ,prop,tab return
    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string Category { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //1.创建一个对象
            var product = new Product();
            //2.设置各属性值
            product.ProductId = 12;
            product.Name = "足球";
            product.Description = "限量版";
            product.Price = 100m;

            //使用对象初始化器
            var product1 = new Product
            {
                ProductId = 123,
                Category = "球类",
                Description = "限量版",
                Name = "篮球",
                Price = 120m
            };

            //使用集合初始化器
            int[] arr = new int[3];
            arr[0] = 1;
            arr[1] = 1;
            arr[2] = 3;
            //对比
            var arr2 = new[] { 11, 2, 3, 4, 5 };
            //for (int i = 0; i < 10; i++)
            //{
            //    arr[i] = i;
            //}
            var products = new List<Product>() {//集合初始化器
            new Product{//对象初始化器
                ProductId =123,
                Category ="球类",
                Description ="限量版",
                Name ="篮球",
                Price =120m
            },
            new Product{
                ProductId =123,
                Category ="球类",
                Description ="限量版",
                Name ="篮球",
                Price =120m
            },
            new Product{
                ProductId =123,
                Category ="球类",
                Description ="限量版",
                Name ="篮球",
                Price =120m
            }};
            Console.ReadKey();
        }
    }
}
